package xhwl.hmo.mall.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.bean.OnItemClickListener;
import xhwl.hmo.mall.http.bean.resp.MessageListBean;
import xhwl.pulltorefresh.adapter.BaseAdapter;
import xhwl.pulltorefresh.adapter.ViewHolder;

/**
 * 系统消息列表的adapter
 */
public class MesageAdapter extends BaseAdapter<MessageListBean> {
    private Context context;
    List<MessageListBean> messageListBean;
    OnItemClickListener listener;
    public MesageAdapter(Context context, List<MessageListBean> messageListBean) {
        super(context, R.layout.message_item, messageListBean);
        this.context = context;
        this.messageListBean = messageListBean;

    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener=listener;
    }
    @Override
    public void convert(final ViewHolder holder, final MessageListBean messageListBean) {
        holder.setText(R.id.message_title, messageListBean.getPUSH_TITLE());
        holder.setText(R.id.message_content, messageListBean.getPUSH_CONTENT());
        final TextView message_time = (TextView) holder.getView(R.id.message_time);
        if (!TextUtils.isEmpty(messageListBean.getCREATE_DATE())) {
            message_time.setText(timeFormat(messageListBean.getCREATE_DATE()));
        }
        final LinearLayout content_layout = (LinearLayout) holder.getView(R.id.content_layout);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onItemClick(holder.getLayoutPosition());
                }
            }
        });
    }

    private String timeFormat(String create_date) {
        // 2017-10-18 15:39:01
        String[] dates = create_date.split(" ");
        String[] datess = dates[0].split("-");
        String year = datess[0].substring(2, 4);
        String[] time = dates[1].split(":");
        return year + "/" + datess[1] + "/" + datess[2] + " " + time[0] + ":" + time[1];
    }
}
